package com.zhnc.dangjianpingtaiapp.gui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.zhnc.dangjianpingtaiapp.R;
import com.zhnc.dangjianpingtaiapp.gui.base.BaseActivity;
import com.zhnc.dangjianpingtaiapp.gui.fragment.AffairFragment;
import com.zhnc.dangjianpingtaiapp.gui.fragment.ItemGridFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 主体首页
 */
public class HomeActivity extends BaseActivity {
    private String aName = "五务";
    private String bName = "村/社区";
    private String cName = "党支部";
    private String dName = "我的";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Fragment a = new AffairFragment();

        Fragment b = new ItemGridFragment();

        Fragment c = new DemoFragment();


        Fragment d = new DemoFragment();


        final List<Fragment> fragments = new ArrayList<>();
        fragments.add(a);
        fragments.add(b);
        fragments.add(c);
        fragments.add(d);

        BottomNavigationBar mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

        TextBadgeItem mTextBadgeItem = new TextBadgeItem()
                .setBorderWidth(4)
                .setBackgroundColorResource(R.color.colorAccent)
                .setAnimationDuration(200)
                .setText("3")
                .setHideOnSelect(false);


        ShapeBadgeItem mShapeBadgeItem = new ShapeBadgeItem()
                .setShapeColorResource(R.color.colorPrimary)
                .setGravity(Gravity.TOP | Gravity.END)
                .setHideOnSelect(false);

        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        mBottomNavigationBar //值得一提，模式跟背景的设置都要在添加tab前面，不然不会有效果。
                .setActiveColor(R.color.home2)//选中颜色 图标和文字
                .setInActiveColor(R.color.home1).setScrollBarSize(6);//默认未选择颜色
//                .setBarBackgroundColor(R.color.white);//默认背景色

        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.a1, aName)
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.a2))
                )
                .addItem(new BottomNavigationItem(R.mipmap.b1, bName)
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.b2)))
                .addItem(new BottomNavigationItem(R.mipmap.c1, cName)
                                .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.c2))
//                        .setBadgeItem(mTextBadgeItem)
                )
                .addItem(new BottomNavigationItem(R.mipmap.d1, dName)
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.d2)))
//                .addItem(new BottomNavigationItem(R.mipmap.home_5_2, "社会服务")
//                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.home_5_1)))
                .setFirstSelectedPosition(0)//设置默认选择的按钮
                .initialise();//所有的设置需在调用该方法前完成


        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                Fragment fragment = fragments.get(position);
                re(fragment);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        mBottomNavigationBar.selectTab(0);
//        handler.sendEmptyMessageDelayed(1, 1000);
    }

    protected Fragment currentFragment;

    public void re(Fragment fragment) {
        currentFragment = fragment;
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_home, fragment);
        fragmentTransaction.commit();
    }
}
